import { undoManager, checkSelection } from "../util/Utils.js";

export default {
  // 设置字体
  setFontFamily (designer, fontfamily) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const hot = context.hot;
    const selected = hot.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let oldFontFamily = this.updateFontFamily(designer, startRow, startCol, endRow, endCol, fontfamily);
    undoManager.add({
      redo: function () {
        oldFontFamily = this.updateFontFamily(designer, startRow, startCol, endRow, endCol, fontfamily);
        context.isModify = true;
      },
      undo: function () {
        for (let i = startRow; i <= endRow; i++) {
          for (let j = startCol; j <= endCol; j++) {
            const cellDef = context.getCell(i, j);
            if (!cellDef) {
              continue;
            }
            const cellStyle = cellDef.cellStyle;
            cellStyle.fontFamily = oldFontFamily[i + "," + j];
            this.fontFamily = cellStyle.fontFamily;
          }
        }
        hot.render();
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  updateFontFamily (designer, startRow, startCol, endRow, endCol, fontFamily) {
    const { context } = designer;
    const hot = context.hot;
    const oldFontFamily = {};
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldFontFamily[i + "," + j] = cellStyle.fontFamily;
        cellStyle.fontFamily = fontFamily;
        this.fontFamily = fontFamily;
      }
    }
    hot.render();
    return oldFontFamily;
  },
  // 设置字体大小
  setFontSize (designer, fontsize) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const hot = context.hot;
    const selected = hot.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let oldFontSize = this.updateFontSize(designer, startRow, startCol, endRow, endCol, fontsize);
    undoManager.add({
      redo: function () {
        oldFontSize = this.updateFontSize(designer, startRow, startCol, endRow, endCol, fontsize);
        context.isModify = true;
      },
      undo: function () {
        for (let i = startRow; i <= endRow; i++) {
          for (let j = startCol; j <= endCol; j++) {
            const cellDef = context.getCell(i, j);
            if (!cellDef) {
              continue;
            }
            const cellStyle = cellDef.cellStyle;
            cellStyle.fontSize = oldFontSize[i + "," + j];
            this.fontSize = cellStyle.fontSize;
          }
        }
        hot.render();
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  updateFontSize (designer, startRow, startCol, endRow, endCol, size) {
    const { context } = designer;
    const hot = context.hot;
    const oldFontSize = {};
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldFontSize[i + "," + j] = cellStyle.fontSize;
        cellStyle.fontSize = size;
        this.fontSize = size;
      }
    }
    hot.render();
    return oldFontSize;
  },
  // 设置粗体
  setBold (designer) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const table = context.hot;
    const selected = table.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let oldBoldStyle = this.updateCellsBoldStyle(designer, startRow, startCol, endRow, endCol);
    undoManager.add({
      redo: function () {
        oldBoldStyle = this.updateCellsBoldStyle(designer, startRow, startCol, endRow, endCol);
        context.isModify = true;
      },
      undo: function () {
        for (let i = startRow; i <= endRow; i++) {
          for (let j = startCol; j <= endCol; j++) {
            const cellDef = this.context.getCell(i, j);
            if (!cellDef) {
              continue;
            }
            const cellStyle = cellDef.cellStyle;
            const bold = oldBoldStyle[i + "," + j];
            cellStyle.bold = bold;
          }
        }
        table.render();
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  updateCellsBoldStyle (designer, startRow, startCol, endRow, endCol) {
    const { context } = designer;
    const hot = context.hot;
    const oldBoldStyle = {};
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldBoldStyle[i + "," + j] = cellStyle.bold;
        if (cellStyle.bold) {
          cellStyle.bold = false;
        } else {
          cellStyle.bold = true;
        }
      }
    }
    hot.render();
    return oldBoldStyle;
  },
  // 设置斜体
  setItalic (designer) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const hot = context.hot;
    const selected = hot.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let oldItalicStyle = this.updateCellsItalicStyle(designer, startRow, startCol, endRow, endCol);
    undoManager.add({
      redo: function () {
        oldItalicStyle = this.updateCellsItalicStyle(designer, startRow, startCol, endRow, endCol);
        context.isModify = true;
      },
      undo: function () {
        for (let i = startRow; i <= endRow; i++) {
          for (let j = startCol; j <= endCol; j++) {
            const cellDef = context.getCell(i, j);
            if (!cellDef) {
              continue;
            }
            const cellStyle = cellDef.cellStyle;
            const italic = oldItalicStyle[i + "," + j];
            cellStyle.italic = italic;
          }
        }
        hot.render();
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  updateCellsItalicStyle (designer, startRow, startCol, endRow, endCol) {
    const { context } = designer;
    const hot = context.hot;
    const oldItalicStyle = {};
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldItalicStyle[i + "," + j] = cellStyle.italic;
        if (cellStyle.italic) {
          cellStyle.italic = false;
        } else {
          cellStyle.italic = true;
        }
      }
    }
    hot.render();
    return oldItalicStyle;
  },
  // 设置下划线
  setUnderline (designer) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const table = context.hot;
    const selected = table.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let oldUnderlineStyle = this.updateCellsUnderlineStyle(designer, startRow, startCol, endRow, endCol);
    undoManager.add({
      redo: function () {
        oldUnderlineStyle = this.updateCellsUnderlineStyle(designer, startRow, startCol, endRow, endCol);
        context.isModify = true;
      },
      undo: function () {
        oldUnderlineStyle = this.updateCellsUnderlineStyle(designer, startRow, startCol, endRow, endCol, oldUnderlineStyle);
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  updateCellsUnderlineStyle (designer, startRow, startCol, endRow, endCol, prevUnderLine) {
    const { context } = designer;
    const oldUnderlineStyle = {};
    const hot = context.hot;
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        // const td = hot.getCell(i, j);
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        let underline = cellStyle.underline;
        if (prevUnderLine) {
          underline = prevUnderLine[i + "," + j];
        }
        oldUnderlineStyle[i + "," + j] = underline;
        if (underline) {
          cellStyle.underline = false;
        } else {
          cellStyle.underline = true;
        }
      }
    }
    hot.render();
    return oldUnderlineStyle;
  },
  // 设置字体颜色
  setFontColor (designer, color, isBg) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const hot = context.hot;
    const selected = hot.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let oldForecolorStyle = this.updateCellsForecolorStyle(designer, startRow, startCol, endRow, endCol, color, isBg);
    undoManager.add({
      redo: function () {
        oldForecolorStyle = this.updateCellsForecolorStyle(designer, startRow, startCol, endRow, endCol, color, isBg);
        context.isModify = true;
      },
      undo: function () {
        for (let i = startRow; i <= endRow; i++) {
          for (let j = startCol; j <= endCol; j++) {
            const cellDef = this.context.getCell(i, j);
            if (!cellDef) {
              continue;
            }
            const cellStyle = cellDef.cellStyle;
            const forecolor = oldForecolorStyle[i + "," + j];
            if (isBg) {
              cellStyle.bgcolor = forecolor;
              this.tool.bgcolor = forecolor;
            } else {
              cellStyle.forecolor = forecolor;
              this.tool.forecolor = forecolor;
            }
          }
        }
        hot.render();
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  updateCellsForecolorStyle (designer, startRow, startCol, endRow, endCol, color, isBg) {
    const { context } = designer;
    const hot = context.hot;
    const oldForecolorStyle = {};
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        if (isBg) {
          oldForecolorStyle[i + "," + j] = cellStyle.bgcolor;
          cellStyle.bgcolor = color;
        } else {
          oldForecolorStyle[i + "," + j] = cellStyle.forecolor;
          cellStyle.forecolor = color;
        }
      }
    }
    hot.render();
    return oldForecolorStyle;
  }
};
